package com.zzjson;

import com.zzjson.controller.MyHealthIndicator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Set;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : PACKAGE_NAME</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年11月12日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
@SpringBootApplication
@EnableDiscoveryClient
public class CloudConfigClientApplication {
	private final ContextRefresher contextRefresher;
	private final Environment environment;

	public CloudConfigClientApplication(ContextRefresher contextRefresher, Environment environment) {
		this.contextRefresher = contextRefresher;
		this.environment = environment;
	}

	public static void main(String[] args) {
		SpringApplication.run(CloudConfigClientApplication.class);
	}

	@Scheduled(fixedRate = 5 * 1000, initialDelay = 3000)
	public void autoRefresh() {
		Set<String> refresh =
				contextRefresher.refresh();
		refresh.forEach(property -> {
			System.out.printf("[Thread :%s]当前配置已更新, " +
							"具体 Key: %s ,Value :%s \n",
					Thread.currentThread().getName(),
					property, environment.getProperty(property));
		});
	}

	@Bean
	public MyHealthIndicator myHealthIndicator() {
		return new MyHealthIndicator();
	}
}